/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 01-nov-2012, 11:05:50
 */
package analizador;

import analizador.AnalizadorLexico.Token;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 *
 * @author 
 */
public class Principal extends javax.swing.JFrame {

    private List<Cadenas> listaCadenas;
    private DefaultTableModel defaultTableModel;

    /** Creates new form Principal */
    public Principal() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.listaCadenas = new ArrayList<Cadenas>();
        defaultTableModel = (DefaultTableModel) tablaResultado.getModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaResultado = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        cuadroTexto = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        lbprincipalnombre = new javax.swing.JLabel();
        lbnombre = new javax.swing.JLabel();
        lbmodulo = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnlimpiar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Principal");

        jPanel1.setBackground(java.awt.SystemColor.textHighlight);
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 4, true));

        tablaResultado.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tablaResultado.setFont(new java.awt.Font("Century Gothic", 1, 11)); // NOI18N
        tablaResultado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo Token", "Texto"
            }
        ));
        jScrollPane2.setViewportView(tablaResultado);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 3, true)));
        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        cuadroTexto.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "Texto", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Gothic", 1, 14), new java.awt.Color(153, 0, 0)), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Gothic", 1, 12))); // NOI18N
        cuadroTexto.setPreferredSize(new java.awt.Dimension(10, 100));
        cuadroTexto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cuadroTextoKeyReleased(evt);
            }
        });
        jScrollPane3.setViewportView(cuadroTexto);

        jPanel2.setBackground(java.awt.SystemColor.textHighlight);
        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbprincipalnombre.setFont(new java.awt.Font("Century Gothic", 1, 18));
        lbprincipalnombre.setForeground(new java.awt.Color(255, 255, 255));
        lbprincipalnombre.setText("ANALIZADOR LEXICO");

        lbnombre.setBackground(new java.awt.Color(255, 255, 255));
        lbnombre.setFont(new java.awt.Font("Century Gothic", 1, 14));
        lbnombre.setForeground(new java.awt.Color(255, 255, 255));
        lbnombre.setText("Por: Anibal Gonzalez");

        lbmodulo.setFont(new java.awt.Font("Century Gothic", 1, 14));
        lbmodulo.setForeground(new java.awt.Color(255, 255, 255));
        lbmodulo.setText("Modulo: 9no \"A\"");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(141, Short.MAX_VALUE)
                .addComponent(lbprincipalnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(lbnombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                .addComponent(lbmodulo)
                .addGap(51, 51, 51))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(lbprincipalnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbmodulo)
                    .addComponent(lbnombre))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBackground(java.awt.SystemColor.textHighlight);
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnlimpiar.setFont(new java.awt.Font("Century Gothic", 1, 18));
        btnlimpiar.setText("LIMPIAR");
        btnlimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlimpiarActionPerformed(evt);
            }
        });

        btnsalir.setFont(new java.awt.Font("Century Gothic", 1, 18));
        btnsalir.setText("SALIR");
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(btnlimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                .addComponent(btnsalir, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnsalir, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                    .addComponent(btnlimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void cuadroTextoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cuadroTextoKeyReleased
            int indice = 0;
            String textoPanel = cuadroTexto.getText();
            AnalizadorLexico analizadorLexico = new AnalizadorLexico();
            MutableAttributeSet modificarColor = new SimpleAttributeSet();
            //cambia color de texto
            listaCadenas.clear();

            StringTokenizer stringTokenizer = new StringTokenizer(textoPanel);
            //Divide un string en substrings delimitados por un espacio
            while (stringTokenizer.hasMoreTokens()) {
                //comprueba si hemos extraído todos los elementos de la misma.
                String subcadena = stringTokenizer.nextToken();//extrae el token actual
                Cadenas cad = new Cadenas(analizadorLexico.validarString(subcadena), subcadena);
                //envio metodo(validarString y texto)
                listaCadenas.add(cad);


                if (analizadorLexico.validarString(subcadena).equals(Token.TokenVariable)) {
                    StyleConstants.setForeground(modificarColor, Color.BLACK);//color al negro al texto
                } else {
                    if (analizadorLexico.validarString(subcadena).equals(Token.TokenEntero)) {
                        StyleConstants.setForeground(modificarColor, Color.BLUE);// color azul al texto
                    } else {
                        if (analizadorLexico.validarString(subcadena).equals(Token.TokenReal)) {
                            StyleConstants.setForeground(modificarColor, Color.ORANGE);// color naranja al texto
                        } else {
                            StyleConstants.setForeground(modificarColor, Color.RED);// color rojo al texto
                        }
                    }
                }
                

                int cont = 0;
                for (int i = 0; i < textoPanel.indexOf(subcadena, indice); i++) {
                    Character c = textoPanel.charAt(i);
                    if (c.equals('\n')) {
                        cont++;
                    }
                }

                cuadroTexto.getStyledDocument().setCharacterAttributes(textoPanel.indexOf(subcadena, indice) - cont, subcadena.length(), modificarColor, true);
                indice = textoPanel.indexOf(subcadena, indice) + subcadena.length();


                //Tabla
                Object[] fila = new Object[2];
                DefaultTableModel modeloTabla = (DefaultTableModel) tablaResultado.getModel();
                while (modeloTabla.getRowCount() > 0) {
                    modeloTabla.removeRow(0);//vacia contenido de tabla
                }
                for (int x = 0; x < listaCadenas.size(); x++) {
                    fila[0] = listaCadenas.get(x).getToken();
                    fila[1] = listaCadenas.get(x).getCadena();
                    defaultTableModel.addRow(fila);
                }
                tablaResultado.updateUI();
            }
}//GEN-LAST:event_cuadroTextoKeyReleased

        private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
            // TODO add your handling code here:
            this.dispose();
        }//GEN-LAST:event_btnsalirActionPerformed

        private void btnlimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlimpiarActionPerformed
            // TODO add your handling code here:
            cuadroTexto.setText(" ");
            DefaultTableModel modelo = (DefaultTableModel) tablaResultado.getModel();
            int numfilas = tablaResultado.getRowCount();
            for (int i = 0; numfilas > i; i++) {
                modelo.removeRow(0);
            }
        }//GEN-LAST:event_btnlimpiarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlimpiar;
    private javax.swing.JButton btnsalir;
    private javax.swing.JTextPane cuadroTexto;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbmodulo;
    private javax.swing.JLabel lbnombre;
    private javax.swing.JLabel lbprincipalnombre;
    private javax.swing.JTable tablaResultado;
    // End of variables declaration//GEN-END:variables
}
